<?php
include 'config.php';

// 获取所有公告
$sql_notices = "SELECT id, notice_title, publish_time FROM notices ORDER BY publish_time DESC";
$result_notices = $conn->query($sql_notices);
$notices = [];
if ($result_notices->num_rows > 0) {
    while ($row = $result_notices->fetch_assoc()) {
        $notices[] = $row;
    }
}

// 获取所有轮播图
$sql_slides = "SELECT * FROM slides";
$result_slides = $conn->query($sql_slides);
$slides = [];
if ($result_slides->num_rows > 0) {
    while ($row = $result_slides->fetch_assoc()) {
        $slides[] = $row["image_url"];
    }
}

// 获取所有按钮
$sql_buttons = "SELECT * FROM buttons";
$result_buttons = $conn->query($sql_buttons);
$buttons = [];
if ($result_buttons->num_rows > 0) {
    while ($row = $result_buttons->fetch_assoc()) {
        $buttons[] = $row;
    }
}
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <title>移动服务中心</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/swiper@8/swiper-bundle.min.css">
    <link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600&display=swap">
    <style>
        :root {
            --primary-color: #6c5ce7;
            --secondary-color: #4a90e2;
            --accent-color: #ff7675;
            --base-font-size: clamp(14px, 3.5vw, 16px); /* 动态字体大小 */
        }

        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Inter', 'PingFang SC', 'Helvetica Neue', sans-serif;
        }

        body {
            background: #f8f9ff;
            min-height: 100vh;
            display: flex;
            flex-direction: column;
            font-size: var(--base-font-size);
        }

        .container {
            padding: 2vh 4vw;
            width: 100%;
            max-width: 600px;
            margin: 0 auto;
            flex: 1;
        }

        /* 轮播图自适应 */
        .swiper {
            width: 100%;
            height: 35vh;
            min-height: 180px;
            max-height: 220px;
            border-radius: 2.5vh;
            overflow: hidden;
            box-shadow: 0 1vh 2vh rgba(108, 92, 231, 0.1);
            margin-bottom: 2vh;
        }

        /* 修改轮播图背景图片显示方式 */
        .swiper-slide {
            background-size: cover; /* 将背景图片设置为完全覆盖容器 */
            background-position: center;
            background-repeat: no-repeat;
        }

        /* 公告区域响应式 */
        .notice-board {
            background: white;
            border-radius: 2vh;
            padding: 2vh 3vw;
            margin-bottom: 3vh;
            box-shadow: 0 0.5vh 1.5vh rgba(0,0,0,0.05);
        }

        .notice-title {
            color: var(--primary-color);
            font-size: 1.2em;
            margin-bottom: 1.5vh;
            display: flex;
            align-items: center;
            gap: 1vw;
        }

        .notice-list {
            list-style: none;
            max-height: 25vh;
            overflow-y: auto;
        }

        .notice-item {
            padding: 1.2vh 0;
            border-bottom: 1px solid #f0f0f0;
            color: #666;
            display: flex;
            align-items: center;
            gap: 2vw;
            font-size: 0.95em;
        }

        .notice-item a {
            color: #666;
            text-decoration: none;
        }

        .notice-item a:hover {
            text-decoration: underline;
        }

        /* 按钮网格响应式 */
        .button-grid {
            display: grid;
            grid-template-columns: repeat(2, 1fr);
            gap: 2vh 3vw;
            margin-top: 3vh;
        }

        /* 前两个按钮样式 */
        .button-grid a:nth-child(-n+2) {
            grid-column: span 2;
            min-height: 10vh; /* 减小最小高度 */
            padding: 1.5vh 1vw; /* 减小内边距 */
            border-radius: 1.5vh; /* 减小圆角 */
            text-align: center;
            transition: all 0.3s ease;
            box-shadow: 0 0.3vh 1vh rgba(108, 92, 231, 0.1); /* 减小阴影 */
            border: 1px solid var(--primary-color); /* 添加边框，颜色为主题色 */
            cursor: pointer;
            display: flex;
            flex-direction: row;
            justify-content: center;
            align-items: center;
            background: white;
            text-decoration: none; /* 去除下划线 */
        }

        .button-grid a:nth-child(-n+2):hover {
            transform: scale(1.03); /* 减小悬停时的缩放比例 */
            box-shadow: 0 0.5vh 1.5vh rgba(108, 92, 231, 0.2);
        }

        .button-grid a:nth-child(-n+2):active {
            transform: scale(0.98);
            background-color: #f0f0f0;
            box-shadow: 0 0.2vh 0.5vh rgba(108, 92, 231, 0.1);
        }

        .button-grid a:nth-child(-n+2) img,
        .button-grid a:nth-child(-n+2) i {
            width: 30px; /* 图标大小 */
            height: 30px;
            margin-right: 0.8vh; /* 调整图标与文字间距 */
            margin-bottom: 0;
        }

        .button-grid a:nth-child(-n+2) span {
            font-size: 1.1em; /* 调大字体大小 */
            color: #1a202c; /* 加深字体颜色 */
            font-weight: 600; /* 增加字体加粗效果 */
        }

        /* 后面按钮样式 */
        .button-grid a:nth-child(n+3) {
            min-height: 10vh;
            padding: 1.5vh; /* 减小内边距 */
            border-radius: 2vh;
            text-align: center;
            transition: all 0.3s ease;
            box-shadow: 0 0.5vh 1.5vh rgba(108, 92, 231, 0.1);
            border: 1px solid var(--primary-color); /*添加边框，颜色为主题色 */
            cursor: pointer;
            display: flex;
            flex-direction: row;
            justify-content: center;
            align-items: center;
            background: white;
            text-decoration: none; /* 去除下划线 */
        }

        .button-grid a:nth-child(n+3):hover {
            transform: scale(1.05);
            box-shadow: 0 0.8vh 2vh rgba(108, 92, 231, 0.2);
        }

        .button-grid a:nth-child(n+3):active {
            transform: scale(0.98);
            background-color: #f0f0f0;
            box-shadow: 0 0.2vh 0.5vh rgba(108, 92, 231, 0.1);
        }

        .button-grid a img {
            width: 30px; /* 图标大小 */
            height: 30px;
            object-fit: contain;
            margin-right: 0.8vh; /* 调整图标与文字间距 */
        }

        .button-grid a span {
            color: #2d3436;
            font-weight: 600; /* 增加字体加粗效果 */
            font-size: 0.95em;
        }

        /* 底部版权 */
        .footer {
            background: var(--primary-color);
            color: white;
            text-align: center;
            padding: 2vh;
            font-size: 0.85em;
            width: 100%;
        }

        /* 响应式调整 */
        @media (orientation: landscape) {
            .container {
                padding: 1.5vh 4vw;
            }
            .swiper {
                height: 45vh;
                max-height: 280px;
            }
            .button-grid {
                grid-template-columns: repeat(3, 1fr);
            }
        }

        @media (max-height: 500px) {
            .button-grid a:nth-child(-n+2) {
                min-height: 15vh;
            }
            .button-grid a:nth-child(n+3) {
                min-height: 15vh;
            }
            .button-grid a img {
                margin-bottom: 0.5vh;
            }
        }

        /* 新增样式：设置日期字体颜色为红色 */
       .publish-time {
            color: red;
        }
    </style>
</head>
<body>
    <div class="container">
        <!-- 轮播图 -->
        <div class="swiper">
            <div class="swiper-wrapper">
                <?php foreach ($slides as $slide): ?>
                    <div class="swiper-slide" style="background-image: url('<?php echo $slide; ?>')"></div>
                <?php endforeach; ?>
            </div>
            <div class="swiper-pagination"></div>
        </div>

        <!-- 公告区域 -->
        <div class="notice-board">
            <div class="notice-title">
                <i class="fas fa-bullhorn"></i>
                最新公告
            </div>
            <ul class="notice-list">
                <?php foreach ($notices as $notice): ?>
                    <li class="notice-item">
                        <a href="notice_detail.php?id=<?php echo $notice['id']; ?>">
                            <?php echo $notice['notice_title']; ?>
                        </a>
                        <span class="publish-time">(<?php echo date('m月d日', strtotime($notice['publish_time'])); ?>)</span>
                    </li>
                <?php endforeach; ?>
            </ul>
        </div>

        <!-- 功能按钮 -->
        <div class="button-grid">
            <?php foreach ($buttons as $button): ?>
                <a href="<?php echo $button['link_url']; ?>" class="nav-button">
                    <?php if ($button['icon_path']): ?>
                        <img src="<?php echo $button['icon_path']; ?>" alt="<?php echo $button['button_text']; ?>">
                    <?php else: ?>
                        <i class="fas fa-coins"></i>
                    <?php endif; ?>
                    <span><?php echo $button['button_text']; ?></span>
                </a>
            <?php endforeach; ?>
        </div>
    </div>

    <!-- 底部版权 -->
    <div class="footer">
        © 寻梦物联科技 2015-<span id="currentYear"></span>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/swiper@8/swiper-bundle.min.js"></script>
    <script>
        // 初始化轮播
        const swiper = new Swiper('.swiper', {
            loop: true,
            autoplay: {
                delay: 3000,
            },
            pagination: {
                el: '.swiper-pagination',
                clickable: true
            },
        });

        // 自动更新年份
        document.getElementById('currentYear').textContent = new Date().getFullYear();

        // 横屏检测
        function checkOrientation() {
            if (screen.orientation) {
                return screen.orientation.type.includes('landscape');
            }
            return window.matchMedia("(orientation: landscape)").matches;
        }

        // 动态调整布局
        function adjustLayout() {
            const container = document.querySelector('.container');
            if (checkOrientation()) {
                container.style.padding = '1.5vh 4vw';
            } else {
                container.style.padding = '2vh 4vw';
            }
        }

        window.addEventListener('resize', adjustLayout);
        window.addEventListener('orientationchange', adjustLayout);
    </script>
</body>
</html>